/**
 * Created by IAN on 2016/10/14.
 */
$(function () {
    var title = "";
    var sel = $.Params.create();
    sel.addParams("page",0);
    sel.addParams("rows",rows);
    getData();

    //查询
    $("#searchBtn").on("click",function(){
        var searchVal = $("#searchVal").val();
        title = searchVal;
        getData();
    })

    //判断checkbox状态
    $("#selAll").on("click",function () {
        if(!$(this).attr("checked")){
            $("input[type=checkbox]").attr("checked","checked")
        }else{
            $("input[type=checkbox]").removeAttr("checked");
        }
    })

    //添加事件
    $('#addDataBtn').on("click",function(){
        window.location.href="../shareExchange/addShare.html";
    });

    //删除事件
    $('#delBtn').on("click",function(){
        var pararms = getCheckData();
        delData(pararms);
    });

    //获取数据
    function getData(){
        getPageData(sel,getIP()+"answers/getAnswersPcList?status=10051001&title="+title,function(data){
            if(data.success){
                var html = "";
                $.each(data.result, function(i,val) {
                    var toTop;
                    if(val.topTime != "0"){
                        toTop = "<button type='button' class='btn btn-primary' uid='"+val.id+"' onclick='cancelTop(this)'>取消置顶</button>";
                    }else{
                        toTop = "<button type='button' class='btn btn-primary' uid='"+val.id+"' onclick='beTop(this)'>置顶</button>";
                    }

                    html+="<tr role='row'' class='even'><td><input eid='"+val.id+"' onclick='getCheck(this)' type='checkbox' class='classOption'></td>"+
                          "<td class='sorting_1'>"+val.TITLE+"</td><td>"+val.name+"</td><td>"+val.hospital_name+" - "+val.hospital_offices+"</td>"+
                          "<td><a href='"+getIP()+"QA/questionManage_info.html?qid="+val.id+"'>"+val.aCount+"</a></td><td>"+val.CREATE_TIME+"</td><td>"+toTop+"</td></tr>";
                });
                $("#listContainer").html(html);
            }
        })
    }

    //删除数据
    function delData(ids){
        if(!ids == ""){
            var del = $.Params.create();
            del.addParams("id",ids)
            del.ajax(getIP()+"answers/delAnswers",function(data){
                if(data.success){
                    alert("删除成功！");
                    getData();
                }
            })
        }else{
            alert("请至少选择一项");
        }
    }

    //置顶
    beTop = function (obj) {
        var del = $.Params.create();
        var qid = $(obj).attr("uid");
        del.ajax(getIP()+"answers/updAnswers?id="+qid+"&topTime="+decodeURI(getNowDateTime()),function(data){
            if(data.success){
                alert("置顶成功！");
                getData();
            }
        })
    }

    //取消置顶
   cancelTop = function (obj) {
       var del = $.Params.create();
       var qid = $(obj).attr("uid");
       del.ajax(getIP()+"answers/updAnswers?id="+qid+"&topTime=0",function(data){
           if(data.success){
               alert("取消置顶成功！");
               getData();
           }
       })
    }
});
